#pragma once

#include <wx/treectrl.h>
#include "prs_DataTree.h"
#include "prs_DataRegTables.h"
#include "prs_DataFolders.h"

namespace app {			 
///////////////////////////////////////////////////////////////////////////////

struct TSystemTables
{
	static const prs::TTableId Explorer;
	static const prs::TTableId Folders;
	static const prs::TTableId RegTables;
};

///////////////////////////////////////////////////////////////////////////////

struct TTreeItemData : public wxTreeItemData
{
	prs::TDataTreeRecord DataTreeRecord;
	//
	TTreeItemData(prs::TDataTreeRecord Record) :DataTreeRecord(Record) {}
};

///////////////////////////////////////////////////////////////////////////////

struct TTreeEvent : public wxNotifyEvent
{
	prs::TDataTreeRecord *DataTreeRecord;
	prs::TDataRegTablesRecord *DataRegTablesRecord;
	prs::TDataFoldersRecord *DataFoldersRecord;
	//
	TTreeEvent(
		wxEventType commandType = wxEVT_NULL,
		int id = 0
	)
	:wxNotifyEvent(commandType, id)
  {
		DataTreeRecord = NULL;
		DataRegTablesRecord = NULL;
		DataFoldersRecord = NULL;
	}
	//
	TTreeEvent(const TTreeEvent& event): wxNotifyEvent(event) {}
	//
	virtual wxEvent *Clone() const { return new TTreeEvent(*this);}
	//
	DECLARE_DYNAMIC_CLASS(TTreeEvent);
};

typedef void (wxEvtHandler::*TTreeEventFunction)(TTreeEvent&);

BEGIN_DECLARE_EVENT_TYPES()									  
	DECLARE_EVENT_TYPE(EVT_COMMAND_OnTreeEvent_SelChanged, 1)
END_DECLARE_EVENT_TYPES()

#define EVT_OnTreeEvent_SelChanged(id, fn) \
	DECLARE_EVENT_TABLE_ENTRY( \
		EVT_COMMAND_OnTreeEvent_SelChanged, id, wxID_ANY, \
		(wxObjectEventFunction)(wxEventFunction) wxStaticCastEvent( TTreeEventFunction, &fn ), \
		(wxObject *) NULL \
	),

///////////////////////////////////////////////////////////////////////////////

struct TTreeCtrl : public wxTreeCtrl
{																					 
	prs::TDataTables &DataTables;
	//
	TTreeCtrl(wxWindow *parent
		,	prs::TDataTables &DataTables
		, wxWindowID id = wxID_ANY
		, const wxPoint& pos = wxDefaultPosition
		, const wxSize& size = wxDefaultSize
		, long style = wxTR_DEFAULT_STYLE
		, const wxValidator &validator = wxDefaultValidator
		, const wxString& name = "TreeCtrl"
	);
	
	//
	TTreeItemData *GetItemData(const wxTreeItemId &item) const { 
	  return dynamic_cast<TTreeItemData*>(wxTreeCtrl::GetItemData(item)); 
	}
	void ConnectData(prs::TDataTree *DataTree);
	void LoadChildren(wxTreeItemId &Parent);
	void UnloadChildren(wxTreeItemId &Parent);

private:
	void OnTreeSelChanged   (wxTreeEvent& event);
	void OnTreeItemExpanding(wxTreeEvent& event);
	void OnTreeItemCollapsed(wxTreeEvent& event);
	//
	prs::TDataTree *dataTree;
};

///////////////////////////////////////////////////////////////////////////////
}
